<template>
  <div class="item-container">
    <!-- 顶部导航 -->
    <div class="top-nav">
      <button class="back-btn" @click="goBack">返回</button>
      <div class="welcome">小淘商城 - 商品详情</div>
    </div>

    <!-- 商品详情区域 -->
    <div class="item-section">
      <!-- 商品主图 -->
      <div class="item-gallery">
        <div class="main-image">
          <img :src="product.imageUrl" :alt="product.name" />
        </div>
      </div>

      <!-- 商品信息和商品介绍 -->
      <div class="item-info-detail">
        <!-- 商品信息 -->
        <div class="item-info">
          <h1 class="item-title">{{ product.name }}</h1>
          <div class="item-meta">
            <div class="item-price">
              <span class="current-price">¥{{ product.price ? product.price.toFixed(2) : '0.00' }}</span>
            </div>
            <div class="item-sales">
              <span>销量: {{ product.sales }}</span>
            </div>
            <div class="item-comments">
              <span>评论: {{ product.comments }}</span>
            </div>
          </div>

          <!-- 操作按钮 -->
          <div class="item-actions">
            <button class="add-to-cart" @click="addToCart">加入购物车</button>
            <button class="buy-now" @click="buyNow">立即购买</button>
          </div>
        </div>

        <!-- 商品详情 -->
        <div class="item-detail">
          <h2 class="detail-title">商品介绍</h2>
          <div class="detail-content">
            <p>这是一款优质商品，欢迎选购！</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getProductDetail } from '@/api/product'
import cartApi from '@/api/cart'
import { type Cart as CartType, type CartItemResponse as CartItemResponseType } from '@/type/cart'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()

// 商品数据
const product = ref({
  id: 0,
  imageUrl: '',
  name: '',
  price: 0,
  sales: 0,
  comments: 0,
  isAd: false
})

const Cart = ref<CartType>({
  productId: 0,
  quantity: 1,
  selected: true
})


// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 获取商品详情
const fetchProductDetail = async () => {
  const productId = Number(route.params.id)
  if (!productId) {
    router.push('/404')
    return
  }

  try {
    const res = await getProductDetail(productId)
    product.value = res.data.data // 适配你的返回格式
    console.log('商品详情:', product.value)
  } catch (error) {
    console.error('获取商品详情失败:', error)
    router.push('/404')
  }
}

// 加入购物车
const addToCart = async () => {
  try {
    Cart.value = {
      productId: product.value.id,
      quantity: 1,
      selected: true
    }
    await cartApi.addCart(Cart.value)
    console.log('加入购物车:', product.value.id)
    ElMessage.success('商品已成功加入购物车')
  } catch (error) {
    console.error('加入购物车失败:', error)
  }
}

// 立即购买
const buyNow = () => {
  router.push({
    path: '/order/confirm',
    query: {
      productId: product.value.id
    }
  })
}

onMounted(() => {
  fetchProductDetail()
})
</script>

<style scoped>
.item-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f8f8f8;
  min-height: 100vh;
}

.top-nav {
  padding: 15px 0;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  gap: 15px;
}

.welcome {
  font-size: 18px;
  color: #333;
  font-weight: 500;
}

.item-section {
  display: flex;
  margin-top: 30px;
  background: #fff;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.item-gallery {
  width: 40%;
  padding-right: 30px;
}

.main-image img {
  width: 100%;
  border-radius: 4px;
  /* 修复负值问题 */
  object-fit: cover;
}

/* 新增：商品信息和商品介绍的父容器 */
.item-info-detail {
  display: flex;
  width: 60%;
}

.item-info {
  width: 50%;
  /* 左侧商品信息占 50% 宽度 */
  padding-right: 30px;
  /* 右侧间距 */
}

.item-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
  font-weight: 600;
}

.item-meta {
  margin-bottom: 30px;
  /* 修复负值问题 */
}

.item-price,
.item-sales,
.item-comments {
  margin-bottom: 15px;
}

.current-price {
  font-size: 28px;
  color: #f56c6c;
  font-weight: bold;
}

.item-sales span,
.item-comments span {
  font-size: 14px;
  color: #666;
}

.item-actions {
  display: flex;
  gap: 15px;
  margin-top: 40px;
}

.add-to-cart,
.buy-now {
  padding: 12px 30px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.add-to-cart {
  background-color: #f0f0f0;
  color: #333;
}

.add-to-cart:hover {
  background-color: #e0e0e0;
}

.buy-now {
  background-color: #f56c6c;
  color: white;
}

.buy-now:hover {
  background-color: #e55c5c;
}

/* 商品介绍部分 */
.item-detail {
  width: 50%;
  /* 右侧商品介绍占 50% 宽度 */
  padding-left: 30px;
  /* 左侧间距 */
}

.detail-title {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.detail-content {
  line-height: 1.6;
  color: #666;
}

.back-btn {
  padding: 5px 10px;
  background-color: #f0f0f0;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.back-btn:hover {
  background-color: #e0e0e0;
}
</style>